package cn.jetpiece.cloud.system.dp.app.modular.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.jetpiece.cloud.core.page.PageFactory;
import cn.jetpiece.cloud.core.query.QueryGenerator;
import cn.jetpiece.cloud.core.util.HttpContext;
import cn.jetpiece.cloud.core.util.MD5Util;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import cn.jetpiece.cloud.system.dp.api.biz.DpTAppApi;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.CreateDpTAppParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.UpdateDpTAppParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.query.DpTAppQuery;
import cn.jetpiece.cloud.system.dp.api.biz.model.result.DpTApiResult;
import cn.jetpiece.cloud.system.dp.api.biz.model.result.DpTAppResult;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTApi;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTApp;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTAppService;
import cn.jetpiece.cloud.system.dp.app.modular.biz.wrapper.DpTAppWrapper;
import cn.jetpiece.cloud.starter.app.modular.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;

/**
 * dp_t_app
 *
 * @author jetpiece
 * @since 2023-05-15
 */
@Slf4j
@RestController
public class DpTAppController extends BaseController<DpTApp, IDpTAppService> implements DpTAppApi {

	@Autowired
	private IDpTAppService dpTAppService;

    @Override
	public Result<String> add(CreateDpTAppParam param) {
	    try {
	        DpTApp dpTApp = BeanUtil.toBean(param, DpTApp.class);
	        if (dpTAppService.save(dpTApp)) {
                return Result.OK("添加成功!");
	        }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("添加失败");
	}

	@Override
    public Result<String> update(UpdateDpTAppParam param) {
	    try {
	        DpTApp oldBean = dpTAppService.getById(param.getId());
	        if (oldBean == null) {
                return Result.error("数据未找到!");
	        }
	        DpTApp dpTApp = BeanUtil.toBean(param, DpTApp.class);
	        if (dpTAppService.updateById(dpTApp)) {
                return Result.OK("编辑成功!");
	        }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("编辑失败");
	}

	@Override
	public Result<String> delete(Long id) {
      try {
            if (dpTAppService.removeById(id)) {
                  return Result.OK("删除成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("删除失败");
	}

	@Override
	public Result<String> deleteBatch(String ids) {
      try {
            List<Long> idList = Convert.toList(Long.class, ids.split(","));
            if (dpTAppService.removeByIds(idList)) {
                  return Result.OK("批量删除成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("批量删除失败");
	}

	@Override
	public Result<DpTAppResult> get(Long id) {
      try {
            DpTApp dpTApp = dpTAppService.getById(id);
            if (dpTApp == null) {
                return Result.error("数据未找到!");
            }
            return Result.OK(BeanUtil.toBean(dpTApp, DpTAppResult.class));
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("获取数据失败！");
	}

    @Override
    public Result<DpTAppResult> init() {
        try {
            String appKey = MD5Util.MD5Encode(UUID.randomUUID().toString(), Charset.defaultCharset().name());
            String appSecret = MD5Util.MD5Encode(UUID.randomUUID().toString(), Charset.defaultCharset().name());
            DpTApp dpTApp = new DpTApp();
            dpTApp.setAppKey(appKey);
            dpTApp.setAppSecret(appSecret);
            return Result.OK(BeanUtil.toBean(dpTApp, DpTAppResult.class));
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("获取数据失败！");
    }

	@Override
	public Result<PageResult<DpTAppResult>> page(DpTAppQuery query) {
	    DpTApp dpTApp = BeanUtil.toBean(query, DpTApp.class);
		QueryWrapper<DpTApp> queryWrapper = QueryGenerator.initQueryWrapper(dpTApp, HttpContext.getRequestParameters());
		Page<DpTApp> page = PageFactory.defaultPage();
		IPage<DpTApp> pageList = dpTAppService.page(page, queryWrapper);
        return Result.OK(new DpTAppWrapper(pageList).setWrapper(true).wrapPageResult());
	}

	@Override
    public Result<String> importExcel(MultipartHttpServletRequest request) {
        return super.importXls(request);
    }

	@Override
    public ModelAndView exportExcel(DpTAppQuery query) {
        return super.exportXls(query, DpTAppResult.class, "dp_t_app");
    }

}
